interface DateFormatOptions {
  year?: string;
  month?: string;
  day?: string;
  hour?: string;
  minute?: string;
  second?: string;
}

// 将 Date 对象格式化为指定格式的字符串
export const formatDate = (
  dateInput: Date | string | number,
  format: string = "YYYY-MM-DD HH:mm:ss"
): string => {
  const date = new Date(dateInput);
  const formatOptions: DateFormatOptions = {
    year: date.getFullYear().toString(),
    month: (date.getMonth() + 1).toString().padStart(2, "0"),
    day: date.getDate().toString().padStart(2, "0"),
    hour: date.getHours().toString().padStart(2, "0"),
    minute: date.getMinutes().toString().padStart(2, "0"),
    second: date.getSeconds().toString().padStart(2, "0"),
  };

  let formattedDate = format;

  formattedDate = formattedDate.replace(/YYYY/g, formatOptions.year ?? "");
  formattedDate = formattedDate.replace(/MM/g, formatOptions.month ?? "");
  formattedDate = formattedDate.replace(/DD/g, formatOptions.day ?? "");
  formattedDate = formattedDate.replace(/HH/g, formatOptions.hour ?? "");
  formattedDate = formattedDate.replace(/mm/g, formatOptions.minute ?? "");
  formattedDate = formattedDate.replace(/ss/g, formatOptions.second ?? "");

  return formattedDate;
};

// 过滤出树结构中type值
export const filterFolders = (treeData: any) => {
  // Helper function to recursively filter the tree nodes
  function recursiveFilter(nodes: any) {
    return nodes
      .filter((node: any) => node.type === "folder") // Filter the nodes that are of type 'folder'
      .map((node: any) => ({
        ...node, // Spread the node's properties
        children: node.children ? recursiveFilter(node.children) : [], // Recursively filter the children if they exist
      }));
  }

  // Start the recursion with the initial tree data
  return recursiveFilter(treeData);
};

export const formatTimestamp = (timestamp: any) => {
  // 创建一个新的 Date 对象
  const date = new Date(timestamp);

  // 获取年、月、日、小时、分钟、秒
  const year = date.getUTCFullYear();
  const month = String(date.getUTCMonth() + 1).padStart(2, "0");
  const day = String(date.getUTCDate()).padStart(2, "0");
  const hours = String(date.getUTCHours()).padStart(2, "0");
  const minutes = String(date.getUTCMinutes()).padStart(2, "0");
  const seconds = String(date.getUTCSeconds()).padStart(2, "0");

  // 返回格式化的字符串
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

/**
 * 删除对象的第n个键
 * @param {Object} obj - 要删除键的对象
 * @param {number} n - 要删除的键的索引
 */
export const deleteNthKey = (obj, n) => {
  // 获取对象的键数组
  const keys = Object.keys(obj);

  // 确保索引在范围内
  if (n >= 0 && n < keys.length) {
    const keyToDelete = keys[n];
    delete obj[keyToDelete];
  } else {
    console.error("索引超出范围");
  }
};

/**
 * 合并多个对象
 * @param {Object[]} objectsArray - 要合并的对象数组
 * @returns {Object} 合并后的对象
 */
export const mergeObjects = (objectsArray) => {
  return objectsArray.reduce((acc, obj) => {
    return { ...acc, ...obj };
  }, {});
};

// 过滤掉树结构的 children 数据，只留下根的函数
export const filterTreeRoot = (data) => {
  return data.map((node) => {
    const { children, ...rest } = node;
    return rest;
  });
};
